package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.Special;
import com.lanou3g.musicenjoy.volley.VolleySingleton;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class SpecialAdapter extends BaseAdapter{
    private List<Special.AlbumlistEntity> datas;
    private Context mContext;
    private ImageLoader imageLoader;

    public SpecialAdapter(Context c){
        mContext=c;
        imageLoader= VolleySingleton.getInstance(mContext).getImageLoader();
    }
    public void initData(List<Special.AlbumlistEntity> datas){
        if (this.datas==null){
            this.datas=new ArrayList<Special.AlbumlistEntity>();
        }else {
            this.datas.clear();
        }
        this.datas=datas;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return datas!=null&&datas.size()>0?datas.size():0;
    }

    @Override
    public Object getItem(int position) {
        return datas!=null&&datas.size()>0?datas.get(position):null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.fragment_special_item,null);
            viewHolder=new ViewHolder();
            viewHolder.imageView= (ImageView) convertView.findViewById(R.id.specialImageView);
            viewHolder.textView= (TextView) convertView.findViewById(R.id.specialTextView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        Special.AlbumlistEntity entity= (Special.AlbumlistEntity) getItem(position);
        if (entity!=null){
            if(entity.getPic_s180()!=null){
            Picasso.with(mContext).load(entity.getPic_s180()).into(viewHolder.imageView);

            }
//            ImageLoader.ImageListener imageListener=ImageLoader.getImageListener(viewHolder.imageView,
//                    R.mipmap.background,R.mipmap.background);
//            imageLoader.get(entity.getPic_small(),imageListener);
            viewHolder.textView.setText(entity.getTitle());
        }
        return convertView;
    }
    private class ViewHolder{
        private ImageView imageView;
        private TextView textView;
    }
}
